package reports.builders;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Collection;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.List;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class PDFBuilder implements IBuilder {

	private String fileName;
	private Document document;
	private PdfPTable table;
	PdfWriter writer;


	public PDFBuilder(String pageTitle)
	{
		this.fileName=pageTitle;

		document = new Document();
		document.setMargins(0,0, 36, 36);

		init();

		document.open();

	}


	private void init()
	{
		//makes sure the reports folder is created
		File reportsFolder= new File("reports");
		if(!reportsFolder.exists())
		{
			reportsFolder.mkdir();
		}
		// creats the file name
		fileName="reports/"+fileName+"-"+System.currentTimeMillis()+".pdf";

		//sets up the writer
		try
		{
			writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
			writer.setPdfVersion(PdfWriter.VERSION_1_7);
		} 
		catch (FileNotFoundException e)
		{

			e.printStackTrace();
		} 
		catch (DocumentException e)
		{

			e.printStackTrace();
		}

	}


	@Override
	public void addTitle(String title) 
	{
		//font creation
		Font font = new Font(FontFamily.COURIER, 25, Font.BOLD);
		Phrase titlePhrase = new Phrase(title,font);

		try
		{
			document.add(titlePhrase);
			document.add( Chunk.NEWLINE );
		} 
		catch (DocumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void startTable(int columns) {
		table = new PdfPTable(columns);

	}

	@Override
	public void endTable() 
	{
		try
		{
			document.add(table);
			document.add( Chunk.NEWLINE );
		} 
		catch (DocumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void addRow(Collection<String> row) {
		Font font = new Font(FontFamily.COURIER, 10, Font.NORMAL);
		for(String element:row)
		{
			PdfPCell cell = new PdfPCell();
			cell.addElement(new Phrase(element,font));
			table.addCell(cell);
		}

	}

	@Override
	public void addTableHeader(Collection<String> headers) 
	{
		Font font = new Font(FontFamily.COURIER, 10, Font.BOLD);
		for(String element:headers)
		{
			PdfPCell cell = new PdfPCell();
			
			cell.addElement(new Phrase(element,font));
			table.addCell(cell);
		}

	}

	@Override
	public void insertPadding(int paddingValue) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addsubTitle(String subTitle) 
	{
		Font font = new Font(FontFamily.COURIER, 15, Font.BOLD);
		Phrase subTitleElement = new Phrase(subTitle,font);

		try
		{
			document.add(subTitleElement);
			document.add( Chunk.NEWLINE );
		} 
		catch (DocumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// TODO Auto-generated method stub

	}

	@Override
	public void addParagraph(String text) {

		Font font = new Font(FontFamily.COURIER, 12, Font.ITALIC);
		Phrase textElement = new Phrase(text,font);

		try
		{
			document.add(textElement);
			document.add( Chunk.NEWLINE );
		} 
		catch (DocumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void saveToFile()
	{
		document.close();
		writer.close();
	}
	
	@Override
	public String getFilePath()
	{
		return fileName;
	}
	@Override
	public void addList(Collection<String> list)
	{
		List listElemen= new List();
		
		for(String element:list)
		{
			listElemen.add(element);
		}
		try
		{
			document.add(listElemen);
			document.add( Chunk.NEWLINE );
		} catch (DocumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}





}
